package com.wt.controller;

import com.wt.pojo.User;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/test")
@RestController
@Api(description = "UserController")
public class ConsumerOrderController {

    @Autowired
    private RestTemplate restTemplate;
    private final  String URL = "http://localhost:8082/user";

    @PostMapping
    public String addUser(User user){
        System.out.println("----------添加--------------user="+user);
        String s = restTemplate.postForObject(URL, user, String.class);
        return  s;
    }

    @PutMapping
    public String updUser(User user){
        System.out.println("----------修改----------user="+user);
        restTemplate.put(URL,user);
        return "修改成功";
    }

    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable("id")Integer id) {
        System.out.println("-----------删除--------------id="+id);
        restTemplate.delete(URL+"/"+id);
    }

    @GetMapping
    public String listUser(Integer id,String name,Integer age,String birthday){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("name",name);
        map.put("age",age);
        String result = restTemplate.getForObject(URL+"?id="+id+"&age="+age+"&name="+name+"&birthday="+birthday, String.class, map);
        System.out.println("get=="+result);
        return result;
    }

/*    @GetMapping("/{name}")
    public String findHello(@PathVariable("name") String uname){
        System.out.println("--------------------------------------------");
        Map<String,String> map = new HashMap<>();
        map.put("name",uname);
        System.out.println("1:"+uname);
        String result = restTemplate.getForObject("http://localhost:8082/hello?name={name}", String.class, map);
        return result;
    }*/
}
